﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;


namespace Marmat.Forms
{
    [Designer("Marmat.Forms.Design.DateTimePickerDesigner")]
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(System.Windows.Forms.DateTimePicker))]
    public partial class DateTimePicker : DropDownBox
    {
        public DateTimePicker() : base()
        {
            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this.ShowModalButton = false;
            this.AllowEdit = true;
            this.TextEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTime;
            this.TextEdit1.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.DateTimeFormat = DateTimeFormatEnum.Date;
            this.TextEdit1.Properties.PopupSizeable = false;
            this.TextEdit1.Properties.ShowPopupCloseButton = false;
            this.TextEdit1.Properties.ShowPopupShadow = false;
        }

        private DateTime _value;

        public new DateTime Value
        {
            get
            {
                var result = this._value;
                if (BaseEdit.EditValue is not null && !ReferenceEquals(BaseEdit.EditValue, DBNull.Value))
                {
                    result = Convert.ToDateTime(BaseEdit.EditValue);
                }
                else if (ReferenceEquals(BaseEdit.EditValue, DBNull.Value))
                {
                    result = default;
                }

                return result;
            }

            set
            {
                this._value = value;
                if (value != default)
                {
                    BaseEdit.EditValue = value;
                }
                else
                {
                    BaseEdit.EditValue = DBNull.Value;
                }
            }
        }

        /// <summary>
        /// 获取或设置日期格式。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(typeof(DateTimeFormatEnum), "Date")]
        public DateTimeFormatEnum DateTimeFormat
        {
            get
            {
                return this.m_DateTimeFormat;
            }

            set
            {
                this.m_DateTimeFormat = value;
                switch (value)
                {
                    case DateTimeFormatEnum.Date:
                        this.TextEdit1.Properties.Mask.EditMask = "d";
                        this.TextEdit1.Properties.DisplayFormat.FormatString = "d";
                        break;
                    case DateTimeFormatEnum.DateLongTime:
                        this.TextEdit1.Properties.Mask.EditMask = "G";
                        this.TextEdit1.Properties.DisplayFormat.FormatString = "G";
                        break;
                    case DateTimeFormatEnum.DateTime:
                        this.TextEdit1.Properties.Mask.EditMask = "g";
                        this.TextEdit1.Properties.DisplayFormat.FormatString = "g";
                        break;
                    case DateTimeFormatEnum.LongTime:
                        this.TextEdit1.Properties.Mask.EditMask = "T";
                        this.TextEdit1.Properties.DisplayFormat.FormatString = "T";
                        break;
                    case DateTimeFormatEnum.Time:
                        this.TextEdit1.Properties.Mask.EditMask = "t";
                        this.TextEdit1.Properties.DisplayFormat.FormatString = "t";
                        break;
                    case DateTimeFormatEnum.YearMonth:
                        this.TextEdit1.Properties.Mask.EditMask = "y";
                        this.TextEdit1.Properties.DisplayFormat.FormatString = "y";
                        break;
                }
            }
        }

        private DateTimeFormatEnum m_DateTimeFormat;

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override PopupControl PopupControl
        {
            get
            {
                if (base.PopupControl is null)
                {
                    base.PopupControl = new DateTimePicker_PopupControl();
                }
                return base.PopupControl;
            }
            set
            {
                base.PopupControl = value;
            }
        }

        private void BDateTimePicker_DropDownFormOpening(object sender, CancelEventArgs e)
        {
            DateTimePicker_PopupControl popup = (DateTimePicker_PopupControl)this.PopupControl;
            popup.calendarControl1.EditValue = this.Value;
            if (this.DateTimeFormat == DateTimeFormatEnum.YearMonth)
            {
                popup.calendarControl1.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView;
            }
            else
            {
                popup.calendarControl1.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.Default;
            }
        }
    }
}